package com.huixuebao.after.service.impl;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huixuebao.after.dto.LoginDTO;
import com.huixuebao.after.entity.CaptchaEntity;
import com.huixuebao.after.entity.UserEntity;
import com.huixuebao.after.mapper.CaptchaMapper;
import com.huixuebao.after.mapper.UserMapper;
import com.huixuebao.after.service.CaptchaService;
import com.huixuebao.after.util.CaptchaUtil;
import com.huixuebao.common.bean.ApiResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Slf4j
@Service
public class CaptchaServiceImpl implements CaptchaService {

    @Autowired
    private CaptchaMapper captchaMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public ApiResult checkMobile(LoginDTO loginDTO) {
        ApiResult apiResult = new ApiResult();
        try{
            UserEntity userEntity = new UserEntity();
            String phone = userEntity.getMobileNo();
            String captchaValue = loginDTO.getCaptchaValue();
            if(checkCaptcha(null,CaptchaUtil.CAPTCHA_TYPE_MOBILE,captchaValue,phone)){

                userEntity.setMobileNo(phone);
                userEntity = userMapper.selectOne(new QueryWrapper<>(userEntity));
                if(userEntity != null){
                    apiResult.setSuccess(true);
                    apiResult.setData(userEntity.getId());
                }else {
                    apiResult.setSuccess(false);
                    apiResult.setMessage("手机号码未注册!!");
                }
            }else {
                apiResult.setSuccess(false);
                apiResult.setMessage("请输入正确到验证码!!");
            }
        }catch (Exception e){
            log.info("查询手机验证码异常!!");
            apiResult.setSuccess(false);
            apiResult.setMessage("checkMobile:" + e.getMessage());
        }
        return apiResult;
    }

    /**
     * @Title checkCaptcha
     * @Description:校验验证码
     */
    @Override
    public boolean checkCaptcha(String captchaUid,String captchaType,String captchaValue,String mobileNo){
        String createTime =  DateUtil.formatDateTime(DateUtil.offset(new Date(), DateField.MINUTE,-5));
        CaptchaEntity captchaEntity = new CaptchaEntity();
        captchaEntity.setCaptchaType(captchaType);
        captchaEntity.setCaptchaValue(captchaValue.toUpperCase());
        captchaEntity.setCaptchaUid(captchaUid);
        if(CaptchaUtil.CAPTCHA_TYPE_MOBILE.equals(captchaType)){
            captchaEntity.setPhone(mobileNo);
        }
        QueryWrapper<CaptchaEntity> queryWrapper = new QueryWrapper<>(captchaEntity);
        queryWrapper.gt("create_time",createTime);
        CaptchaEntity captcha = this.captchaMapper.selectOne(queryWrapper);
        if(captcha!=null){
            //验证码校验之后进行删除，保证验证码只能使用一次(暂时屏蔽-0401)
            //this.removeById(captcha);
            return true;
        }else{
            return false;
        }
    }


}
